package com.sise.wangzhan.command;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * @Classname ObservableCommandHello
 * @Description TODO
 * @Date 2020/4/22 22:09
 * @Created by wangzhan
 */
public class ObservableCommandHello extends HystrixObservableCommand<String> {

    private final String name;

    public ObservableCommandHello(String name){
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }

    @Override
    protected Observable<String> construct() {

        return Observable.create(new Observable.OnSubscribe<String>(){
            @Override
            public void call(Subscriber<? super String> observer){
                if (!observer.isUnsubscribed()){
                    observer.onNext("Hello ");
                    observer.onNext(name + "! thread:");
                    observer.onNext(Thread.currentThread().getName());
                    observer.onCompleted();
                }
            }
        }).subscribeOn(Schedulers.io());
    }
}
